package org.mbigger;

import javax.script.*;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();

        // Get the list of all available engines
        List<ScriptEngineFactory> list = manager.getEngineFactories();

        // Print the details of each engine
        for (ScriptEngineFactory f : list) {
            System.out.println("Engine Name:" + f.getEngineName());
            System.out.println("Engine Version:" + f.getEngineVersion());
            System.out.println("Language Name:" + f.getLanguageName());
            System.out.println("Language Version:" + f.getLanguageVersion());
            System.out.println("Engine Short Names:" + f.getNames());
            System.out.println("Mime Types:" + f.getMimeTypes());
            System.out.println("===");
        }


        ScriptEngine engine = manager.getEngineByName("JavaScript");

        if (engine instanceof Compilable) {
            System.out.println("Script compilation is supported.");
        } else {
            System.out.println("Script compilation is not supported.");
        }

        Compilable comp = (Compilable) engine;
        CompiledScript cScript = comp.compile("print(n1 + n2)");

        Bindings scriptParams = engine.createBindings();
        scriptParams.put("n1", 2);
        scriptParams.put("n2", 3);
        cScript.eval(scriptParams);

        scriptParams.put("n1", 9);
        scriptParams.put("n2", 7);
        cScript.eval(scriptParams);
    }
}
